package com.joywifi.android.demo.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.joywifi.android.demo.R;

import butterknife.Bind;
import butterknife.ButterKnife;

public class MainViewPagerFragment extends Fragment {

    private View mView;
    private ViewPager mViewPager;
    private TabLayout mTabLayout;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.fragement_main_view_pager, container, false);
        mViewPager = (ViewPager) mView.findViewById(R.id.viewPager);
        mTabLayout = (TabLayout) mView.findViewById(R.id.tabLayout);

        MainViewTabFragmentPagerAdapter fragmentAdapter = new MainViewTabFragmentPagerAdapter(getChildFragmentManager(), getActivity());
        mViewPager.setAdapter(fragmentAdapter);//给ViewPager设置适配器
        mTabLayout.setupWithViewPager(mViewPager);//将TabLayout和ViewPager关联起来。
        mTabLayout.setTabsFromPagerAdapter(fragmentAdapter);//给Tabs设置适配器
        return mView;
    }

    public class MainViewTabFragmentPagerAdapter extends FragmentPagerAdapter {
        private String[] titles = new String[]{"Tab1", "Tab2", "Tab3", "Tab4"};
        private Context context;

        public MainViewTabFragmentPagerAdapter(FragmentManager fm, Context context) {
            super(fm);
            this.context = context;
        }

        @Override
        public Fragment getItem(int position) {
            return MainViewTabPageFragment.newInstance(position + 1);
        }

        @Override
        public int getCount() {
            return titles.length;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return titles[position];
        }
    }

    public static class MainViewTabPageFragment extends Fragment {

        public static final String ARGS_PAGE = "args_page";
        @Bind(R.id.id_textview)
        TextView textView;
        private int mPage;

        public static MainViewTabPageFragment newInstance(int page) {
            Bundle args = new Bundle();
            args.putInt(ARGS_PAGE, page);
            MainViewTabPageFragment fragment = new MainViewTabPageFragment();
            fragment.setArguments(args);
            return fragment;
        }

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            mPage = getArguments().getInt(ARGS_PAGE);
        }

        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.fragment_tab_text_item, container, false);
            ButterKnife.bind(this, view);
            textView.setText("第" + mPage + "页");
            return view;
        }
    }
}
